<template>
  <div class="login-container">
    <img src="@/assets/images/login_logo.png" class="logo" alt="">
    <div class="login_wrap">
      <div class="login_img">
        <div class="h3">专业的智慧场馆运营管理系统平台</div>
        <div class="text">多服务端口，满足场馆多场景服务需求</div>
      </div>
      <div class="form_wrap">
        <h3 class="title">{{ title }}</h3>
        <div class="label">用户名</div>
        <div class="line">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入账号"
            type="text"
            tabindex="1"
          />
        </div>
        <div class="label">密码</div>
        <div class="line">
          <!-- <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            :type="passwordType"
            tabindex="1"
          /> -->
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            show-password
            @keyup.enter.native="handleLogin"
          />
        </div>
        <div class="label">验证码</div>
        <div class="line">
          <el-input
            v-model="loginForm.codeImage"
            placeholder="请输入验证码"
            @keyup.enter.native="handleLogin"
          />
          <img
            class="code_image"
            :src="codeImage"
            alt=""
            @click="refreshCodeImage"
          >
        </div>
        <div class="line forget primaryColor">
          <el-checkbox v-model="isRemPsd">
            <span style="font-size: 13px;">记住密码</span>
          </el-checkbox>
          <!-- <span class="text">忘记密码</span> -->
        </div>
        <el-button
          :loading="loading"
          type="primary"
          class="login_btn"
          @click.native.prevent="handleLogin"
        >立即登录</el-button>
        <!-- <div class="agreement mt20">
          <span style="color:#888888">登录即代表同意</span>
          <span class="primaryColor pointer">《豆米跳跳用户协议》</span>
        </div> -->
        <div class="google">
          <img src="@/assets/images/ic_google.png" class="img" alt="">
          <span class="placeholder6">本系统推荐使用GOOGLE浏览器</span>
        </div>
      </div>
    </div>
    <div class="login_bottom">
      <div>—— 豆米跳跳，专业的运动场馆智慧运营管理系统平台 ——</div>
      <div class="copyright">Copyright © 2022 - 2023 豆米跳跳. All Rights Reserved.  版权所有</div>
    </div>
  </div>
</template>

<script>
import { codeImageUrl, login } from '@/api'
export default {
  name: 'Login',
  data() {
    return {
      title: '登录豆米跳跳 - 平台端',
      showForm: false,
      loginForm: {
        username: '',
        password: '',
        codeImage: ''
      },
      loading: false,
      isRemPsd: false,
      passwordType: 'password',
      redirect: undefined,
      codeImage: codeImageUrl + '?time=' + new Date().getTime()
    }
  },
  mounted() {
    this.loginForm.username = localStorage.getItem('username') || ''
    this.loginForm.password = localStorage.getItem('password') || ''
    const isRemPsd = localStorage.getItem('isRemPsd') || false
    this.isRemPsd = JSON.parse(isRemPsd)
    this.showForm = true
    if (this.$route.query.shopId) sessionStorage.setItem('shopId', this.$route.query.shopId)
  },
  created() {
    console.log(this.codeImage)
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    refreshCodeImage() {
      this.codeImage = codeImageUrl + '?time=' + new Date().getTime()
    },
    handleLogin() {
      const { username, password, codeImage } = this.loginForm
      const that = this
      if (!username) {
        return this.$message.error('请输入用户名')
      }

      if (!password) {
        return this.$message.error('请输入密码')
      }

      if (!codeImage) {
        return this.$message.error('请输入验证码')
      }
      this.loading = true
      login({
        param: {
          userName: username.trim(), password: password, imgCode: codeImage
        }
      })
        .then((res) => {
          that.loading = false
          if (res.errorCode !== '000000') return
          if (this.isRemPsd) {
            localStorage.setItem('username', username)
            localStorage.setItem('password', password)
            localStorage.setItem('isRemPsd', this.isRemPsd)
          } else {
            localStorage.removeItem('username')
            localStorage.removeItem('password')
            localStorage.removeItem('isRemPsd')
          }
          that.$store.dispatch('user/login', res.record)
          that.$router.push({ path: '/' })
          // that.$store.dispatch('user/login', res.record)
          // if (res.record.editPwd === '1') {
          //   that.pwdTip('当前登录密码长期未修改，请重新设置新密码')
          // } else if (res.record.editPwd === '2') {
          //   that.pwdTip('当前登录初始密码强度过弱，请重新设置新密码')
          // }
        })
        .catch(() => {
          that.loading = false
          that.refreshCodeImage()
        })
    },
    pwdTip(txt) {
      this.$confirm(txt, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then((res) => {
        if (res.errorCode !== '000000') return
        this.$router.replace({ name: 'Profile', query: { activename: 'resetPwd' }})
      }).catch(() => {
        this.$router.push({ path: '/' })
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // });
        // this.$router.push({ path: '/venueList' })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #cfe9ff;
  overflow: hidden;
  position: relative;
  background-image: url("../../assets/images/login_bg.png");
  background-size: 100% 100%;
  background-attachment: fixed;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  .login_wrap {
    width: 1000px;
    height: 600px;
    box-sizing: border-box;
    background: #ffffff;
    box-shadow: 0px 20px 60px 0px rgba(0, 0, 0, 0.11);
    display: flex;
    .login_img {
      width: 500px;
      height: 600px;
      background: url("../../assets/images/login_img.png");
      background-size: 100% 100%;
      padding-left: 40px;
      padding-top: 80px;
      .h3{
        font-size: 28px;
        font-weight: 700;
        color: #fff;
        margin-bottom: 26px;
      }
      .text{
        font-size: 18px;
        color: #fff;
      }
    }
    .form_wrap {
      flex: 1;
      height: 100%;
      background-color: #fff;
      box-sizing: border-box;
      padding: 0 80px;
      position: relative;
      .title {
        font-size: 24px;
        margin-top: 70px;
        margin-bottom: 36px;
        text-align: center;
      }
      .label {
        color: #222222;
        margin-bottom: 16px;
        font-size: 14px;
      }
      .line {
        margin-bottom: 20px;
        display: flex;
        .el-input{
          height: 40px;
          // font-size: 14px;
          ::v-deep .el-input__inner{
            height: 40px !important;
          }
        }
        .code_image {
          width: 100px;
          margin-left: 10px;
        }
      }
      .forget{
        justify-content: space-between;
        .text{
          cursor: pointer;
        }
      }
      .login_btn{
        width: 100%;
        height: 40px;
        background: linear-gradient(90deg, #435EBE 0%, #4F7BDF 100%);
        box-shadow: 0px 2px 8px 0px rgba(67,94,190,0.15);
        border-radius: 2px;
        margin: 0 auto;
      }
      .agreement{
        text-align: center;
      }
      .google{
        position: absolute;
        left: 50%;
        transform: translate(-50%,0);
        bottom: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        font-size: 12px;
        .img{
          margin-right: 5px;
          width: 18px;
        }
      }
    }
  }
  .logo {
    position: absolute;
    top: 4%;
    left: 5.6%;
    width: 200px;
  }
  .login_bottom{
    text-align: center;
    position: absolute;
    bottom: 3%;
    left: 50%;
    transform: translate(-50%,0);
    font-size: 14px;
    .copyright{
      color: #828D9E;
      margin-top: 2.5%;
      font-size: 12px;
    }
  }
}
</style>
